1 from tkinter import *
2 import random
3
4 #list of possible colour.
5 colours = ['Red','Blue','Green','Pink','Black',
6 'Yellow','Orange','White','Purple','Brown']
7
8 score = 0
9
10 #To take in account the time left: initially 30 seconds
11 time = 30
12
13 #Function that will start the Game
14 def startGame(event):
15
16 if time==30:
17
18 #start the countdown timer
19 countdown()
20
21 #run the function to chose the next color
22 nextcolor()
23
24 def nextcolor():
25
26 global score
27 global time
28
29 #if a game is in play
30 if time > 0:
31
32 #make the text entry box active
33 colour_entry.focus_set()
34
35 if colour_entry.get().lower() == colours[1].lower():
36
37 score += 1
38
39 #clear the entry the box
40 colour_entry.delete(0, END)
41
42 random.shuffle(colours)
43
44 # change the colour to type, by changing the
45 # text _and_ the colour to a random colour value
46 colour.config(fg= str(colours[1]) , text = str(colours[0]))
47
48 # update the score.
49 scoreLabel.config(text = "Score: " + str(score))
50
51 #Countdown Timer Fuction
52 def countdown():
53
54 global time
55
56 #if a game is in play
57 if time > 0 :
58
59 #decrement the value
60 time -= 1
61
62 # update the time left label
63 timeLabel.config(text = "Time left: "+ str(time))
64
65 # run the function again after 1 second.
66 timeLabel.after(1000, countdown)
67
68 #Driver Code
69 if __name__=='__main__':
70
71 root = Tk()
72
73 #Setting the title
74 root.title('Color Game')
75
76 #Setting the geometry of the window
77 root.geometry('375x200')
78
79 #set an instruction label
80 instructions = Label(root, text = 'Type in the colour of the words, and not the word text!', font = ('Helvetica', 12))
81 instructions.pack()
82
83 #Create a Score label
84 scoreLabel = Label(root, text = 'Score :'+str(score), font=('Helvetica' , 12))
85 scoreLabel.pack()
86
87 #Create a Time Label
88 timeLabel = Label(root, text = 'Time Left : '+str(time), font=('Helvetica' , 12))
89 timeLabel.pack()
90
91 #create a colour label
92 colour = Label(root, font=('Helevetica',12))
93 colour.pack()
94
95 #Entry box for input from user
96 colour_entry = Entry(root)
97
98
99 colour_entry.focus_set()
100 root.bind('<Return>',startGame)
101
102 colour_entry.pack()
103
104 root.mainloop()